/**
 *	@file	telseq_mama.c
 *	@brief	ママ　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "field/zonedata.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_mama.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

static u16 mama_sub_DailyTalkSelect(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk);
static u16 mama_sub_DepositContinueCheck(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk);

/**
 *	@brief	電話　ママの会話抽選
 */
u16 PMTelPlayL_Mama(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	wk->seq_code = TEL_SEQ_SIMPLE;

	//イベント着信
	if(wk->ev_type == TEL_RECEIVE_EVENT){
		//フラグを立てておく
		EventWork_SetEventFlag(man->ev_sv,FE_MAMA_DEPOSIT_MSG);
		wk->seq_code = TEL_SEQ_MAMA_DEPOSIT;
		return 0;
	}

	//同一マップにいるか？
	if(wk->pbk->map_id == man->zone_id)	{
		return TEL_TK_MAMA_NE;
	}
	//ノーマル着信
	if(wk->is_receive){
		wk->seq_code = TEL_SEQ_SIMPLE;
		return wk->ev_id;
	}

	//ノーマル発信
	//ライバル名を入力してない
	if(!EventWork_CheckEventFlag(man->ev_sv,FE_RIVAL_NAME_INPUT)){
		return TEL_TK_MAMA_B_RIVAL_NAME;
	}else if(!EventWork_CheckEventFlag(man->ev_sv,FE_MAMA_DEPOSIT_MSG)){
		return TEL_TK_MAMA_B_DEPOSIT_TALK;
	}
	//通常の貯金確認会話
	wk->seq_code = TEL_SEQ_MAMA_DEPOSIT;
	return 0;
}

/**
 *	@brief	お母さんと貯金の話シーケンス
 */
int PMTelSeq_TypeMamaDeposit(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;

	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		//現在たまっている貯金額展開
		wk->tmp_u32 = PMTelSv_DepositControl(man->tel_sv,PMTEL_DEPOSIT_GET,0);
		WORDSET_RegisterNumber( man->wset, WORDSET_TEL_LOCAL_BUF,
			wk->tmp_u32, 6,	NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
		//貯金中か否か？
		wk->tmp_flag1 = SysFlag_IsDepositEnable(man->ev_sv);
		//アイテムストックに空きがあるか？
		wk->tmp_flag2 = PMTelSv_IsDepositItemStockFull(man->tel_sv);

		if(wk->ev_type == TEL_RECEIVE_EVENT){
			//日常会話は飛ばす
			wk->seq = 2;
			return FALSE;
		}
		//日常会話出力
		PMTel_MsgPrintGmm(man,man->m_person,mama_sub_DailyTalkSelect(man,wk));
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		//次
		if(wk->tmp_flag2){	//ストックがいっぱいの場合
			PMTel_MsgPrintGmm(man,man->m_person,m_mama_stock_full);
			wk->seq = 0xFF;
			return FALSE;
		}
		break;
	case 2:
		//貯金確認メッセージ
		PMTel_MsgPrintGmm(man,man->m_person,mama_sub_DepositContinueCheck(man,wk));
		break;
	case 3:
		//メッセージ再生待ち
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		//選択肢表示
		PMTel_SelBoxSet(man,SBOX_ID_DEPOSIT);
		break;
	case 4:
		//選択待ち
		{
			u32 ret = PMTel_SelBoxMain(man);

			if(ret == SBOX_SELECT_NULL){
				return FALSE;
			}
			PMTel_SelBoxExit(man);

			if(ret == 0){	//貯金する
				wk->tmp_flag2 = 1;
				PMTel_MsgPrintGmm(man,man->m_person,m_mama_deposit_03);
			}else{
				wk->tmp_flag2 = 0;
				PMTel_MsgPrintGmm(man,man->m_person,m_mama_deposit_04);
			}
			SysFlag_DepositSet(man->ev_sv,wk->tmp_flag2);
		}
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}

/*
 *	@brief	お母さんの貯金話　日常会話抽選
 */
static u16 mama_sub_DailyTalkSelect(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
//	if(!wk->tmp_flag2 ){	//ストック領域に空きがあるとき
	return ZoneData_GetTelMamaPlaceMsgID(man->zone_id)+m_mama_place_t20;
//	}
//	return m_mama_stock_full;
}

/*
 *	@brief	貯金継続確認メッセージ抽選
 */
static u16 mama_sub_DepositContinueCheck(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	if(wk->ev_type == TEL_RECEIVE_EVENT){
		return m_mama_deposit_00;
	}
	if(wk->tmp_flag1){	//現在貯金中
		if(wk->tmp_u32){	//貯金額1円以上
			return m_mama_deposit_01;
		}
		return m_mama_deposit_05;
	}
	if(wk->tmp_u32){
		return m_mama_deposit_02;
	}
	return m_mama_deposit_06;
}
